import React from 'react'
import { ROUTER_DICT } from '../dict/routerDict'
import { lazyLoadIcon } from './iconHook'
/**
 * 根据数组生成路由数组，主要是根据routeArys中组件的路径动态的导入组件
 * @param {后端返回的路由数组} routeArys 
 */
let key = 1
export const createRoutesByRequire = (routeArys) => {
    
    return routeArys.map((routeObj) => {
        key++
            if (routeObj.children) {
                let obj = {
                    key:routeObj.name?routeObj.name:key,
                    icon:routeObj.icon?lazyLoadIcon(routeObj.icon):'',
                    label:routeObj.title,
                    children: (routeObj.children && routeObj.children.length > 0)?createRoutesByRequire(routeObj.children):''
                }
                return (obj)
            }else {
                let obj = {
                    key:routeObj.name?routeObj.name:key,
                    icon:routeObj.icon?lazyLoadIcon(routeObj.icon):'',
                    label:routeObj.title,
                }
                return (obj)
            }
            
        });
}
/**
 * 提取所有path 和 element
 * @param {*} routes 
 * @returns 
 */
export function extractPathsAndElements(routes) {
	const pathsAndElements = [];
	routes.forEach(route => {
		const { name, children } = route;
		if (name) {
            const AsyncComponent = React.lazy(() => import(`../view/admin/${ROUTER_DICT[name]}`));
			pathsAndElements.push({ path:'/admin/'+name, element: <AsyncComponent/> });
		}
		if (children) {
			const childPathsAndElements = extractPathsAndElements(children);
			pathsAndElements.push(...childPathsAndElements);
		}
	});
	return pathsAndElements;
  }
  /**
 * 提取所有path 和 element
 * @param {*} routes 
 * @returns 
 */
export function extractPathsAndElementsBykey(routes) {
	const pathsAndElements = [];
	routes.forEach(route => {
		const { name, children } = route;
		if (name) {
            const AsyncComponent = React.lazy(() => import(`../view/admin/${ROUTER_DICT[name]}`));
			pathsAndElements.push({ path:'/admin/'+name, element: <AsyncComponent/> });
		}
		if (children) {
			const childPathsAndElements = extractPathsAndElements(children);
			pathsAndElements.push(...childPathsAndElements);
		}
	});
	return pathsAndElements;
  }



